<template>
  <div class="detail">
    <van-nav-bar
      title="商品详情"
      left-arrow
      @click-left="onClickLeft"
      class="title"
      ><template #right>
        <van-cell @click="showShare = true">
          <template #title>
            <img
              src="https://z3.ax1x.com/2021/07/17/Wl25ZR.png"
              alt=""
              class="img"
            />
          </template>
        </van-cell>
        <van-share-sheet
          v-model:show="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
        />
      </template>
    </van-nav-bar>
    <van-swipe
      class="my-swipe"
      indicator-color="white"
      :loop="false"
      :immediate="false"
    >
      <van-swipe-item>
        <img :src="detaillist[0]?.imgurlone" alt="" />
      </van-swipe-item>
      <van-swipe-item>
        <img :src="detaillist[0]?.imgurltwo" alt="" />
      </van-swipe-item>
      <van-swipe-item>
        <img :src="detaillist[0]?.imgurlthree" alt="" />
      </van-swipe-item>
    </van-swipe>
    <div class="price">
      <div class="flexprice">
        <div>
          <span>￥</span>{{ detaillist[0]?.newprice }}
          <i>{{ detaillist[0]?.oldprice }}</i>
        </div>
        <i>已售{{ detaillist[0]?.sails }}</i>
      </div>
      <p>{{ detaillist[0]?.title }}</p>
    </div>

    <div class="delivery">
      <van-cell is-link title="规格 请选择规格" @click="show2 = true" />
      <van-action-sheet v-model:show="show2">
        <div class="content1">
          <h4>
            <p>{{ allprice }}</p>
            <p>请选择规格属性</p>
          </h4>
          <div class="size">
            <p>规格</p>
            <p>
              <span @click="fn(1)" :class="{ active: state.num == 1 }"
                >【1盒】 21粒 限时价</span
              ><span @click="fn(2)" :class="{ active: state.num == 2 }"
                >【2盒】42粒</span
              >
            </p>
            <p>
              <span @click="fn(3)" :class="{ active: state.num == 3 }"
                >【3盒】 63粒</span
              ><span @click="fn(4)" :class="{ active: state.num == 4 }"
                >【4盒】84粒</span
              >
            </p>
          </div>
          <div class="num">
            <div class="jiajia">
              <span>购买数量</span>
              <li>
                <span @click="decrease">-</span
                ><input type="number" v-model="state.count" /><span
                  @click="increase"
                  >+</span
                >
              </li>
            </div>
          </div>
          <van-action-bar>
            <van-action-bar-button
              type="danger"
              text="确定"
              @click="onClickButton1"
            />
          </van-action-bar>
        </div>
      </van-action-sheet>
      <van-cell is-link title="参数 商品明细" @click="show = true" />
      <van-action-sheet v-model:show="show" title="商品明细">
        <div class="content2">
          <p><span>药品名称</span>艾丽奥利司他胶囊0.12g*7粒*3板，</p>
          <p><span>药品通用名</span>奥利司他胶囊</p>
          <p><span>是否为处方药</span>否</p>
          <p><span>剂型</span>胶囊</p>
          <p><span>性状</span>本品为胶囊剂，内容物为白色或类白色颗粒或粉末。</p>
          <p><span>主要成份</span>本品每粒含奥利司他0.12克</p>
          <van-action-bar-button
            type="danger"
            text="确定"
            @click="onClickButton2"
            class="quren"
          />
        </div>
      </van-action-sheet>
      <van-cell is-link title="配送 物流配送" @click="show1 = true" />
      <van-action-sheet v-model:show="show1">
        <div class="content">
          <h4>此商品支持以下配送方式</h4>
          <p><em>物流配送</em></p>
          <p>
            运费说明:快递配送:计件:满39.00元包邮;1件内8.00元;超出后每1件0.00元;其他区域:
            [ 石家庄市邢台市] 1件内100.00元;超出后每1件100.00元;
          </p>
        </div>
      </van-action-sheet>
    </div>
    <div class="store">
      <span class="left">
        <img :src="detaillist[0]?.shopimg" alt="" />
      </span>
      <div class="center">
        <h4>{{ detaillist[0]?.shoptitle }}</h4>
      </div>
      <div class="right" @click="enter">进店 ></div>
    </div>
    <van-action-bar>
      <van-action-bar-icon
        icon="chat-o"
        text="客服"
        color="#ee0a24"
        @click="onClickIcon"
      />
      <van-action-bar-icon icon="cart-o" text="购物车" @click="onClickIcon" />
      <van-action-bar-icon
        icon="star"
        text="已收藏"
        color="#ff5000"
        @click="onClickIcon"
      />
      <van-action-bar-button
        type="warning"
        text="加入购物车"
        @click="onClickButton"
      />
      <van-action-bar-button
        type="danger"
        text="立即购买"
        @click="onClickButton"
      />
      <van-action-sheet v-model:show="show" title="标题">
        <div class="content">内容</div>
      </van-action-sheet>
    </van-action-bar>
    <div class="innerdetail">
      <h4>详情</h4>
      <img src="https://z3.ax1x.com/2021/07/21/WUyZFJ.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyrTg.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyykQ.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyfXV.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyHh9.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyO6x.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyXX6.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyx0O.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WUyz7D.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WU69tH.png" alt="" />
      <img src="https://z3.ax1x.com/2021/07/21/WU6i9A.png" alt="" />
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import { ref, computed, onMounted, reactive } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

export default {
  setup() {
    const router = useRouter();
    const store = useStore();
    const state = reactive({
      count: 1,
      num: 0,
    });
    //title右上角分享功能
    const showShare = ref(false);
    const detaillist = computed(() => store.state.detaillist);
    const options = [
      { name: "微信", icon: "wechat" },
      { name: "微博", icon: "weibo" },
      { name: "复制链接", icon: "link" },
      { name: "分享海报", icon: "poster" },
      { name: "二维码", icon: "qrcode" },
    ];
    let allprice = computed(
      () =>
        detaillist.value[0].newprice * state.count ||
        detaillist.value[0].newprice
    );

    const decrease = () => {
      state.count--;
      reset();
      if (state.count <= 1) {
        state.count = 1;
      }
    };
    const increase = () => {
      state.count++, reset();
    };
    function reset() {
      if (isNaN(allprice)) {
        allprice = computed(() => detaillist.value[0].newprice);
      } else {
        allprice = computed(() => detaillist.value[0].newprice * state.count);
      }
    }
    const fn = (num) => {
      state.num = num;
      store.commit("changeprice", num);
    };
    const onClickLeft = () => {
      router.go(-1);
    };
    //详情页获取数据
    onMounted(() => {
      getdetaillist(location.hash.split("/").pop());
    });
    const getdetaillist = (hash) => {
      store.dispatch("getdetaillistAsync", { hash });
    };
    //规格参数配送
    const show = ref(false);
    const show1 = ref(false);
    const show2 = ref(false);
    const onSelect1 = (item) => {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      show.value = false;
      Toast(item.name);
    };
    //底部立即购买
    const onClickIcon = () => Toast("点击图标");
    const onClickButton = () => {
      show2.value = true;
    };
    const onClickButton2 = () => {
      show.value = false;
    };
    const onClickButton1 = () => {
      Toast("添加成功");
      show2.value = false;
    };
    return {
      options,
      showShare,
      onClickLeft,
      onClickIcon,
      onClickButton,
      allprice,
      show,
      onSelect1,
      detaillist,
      show1,
      show2,
      onClickButton1,
      onClickButton2,
      state,
      fn,
      decrease,
      increase,
    };
  },
};
</script>
<style lang="less">
.detail {
  background: #fff;
  //商品详情头部
  .title {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 50px;
    .van-nav-bar__title {
      color: #333333;
      font-weight: 400 !important;
    }
    .van-icon-arrow-left {
      width: 10px !important;
      height: 17px !important;
      font-weight: bold;
      color: #000;
      font-size: 16px;
    }
    .van-nav-bar__right {
      padding-right: 4px;
      .van-cell {
        margin-top: 8px;
        background: transparent;
      }
      .img {
        width: 20px;
        height: 20px;
      }
    }
  }
  //轮播图
  .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    height: 375px;
    text-align: center;
    margin-top: 50px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .price {
    border-bottom: 10px solid #f6f7fb;
    padding: 19px 14px 18px 18px;
    height: 92px;
    .flexprice {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 23px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #fb4004;
      line-height: 18px;
      div {
        span {
          font-size: 15px;
          font-weight: 400;
          color: #fb4004;
          line-height: 18px;
        }
        i {
          text-decoration: line-through;
        }
      }
      i {
        font-size: 12px;
        font-weight: 400;
        color: #878787;
        line-height: 18px;
      }
    }
    p {
      font-size: 18px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      line-height: 38px;
    }
  }
  .delivery {
    border-bottom: 10px solid #f6f7fb;
    .van-cell--clickable {
      height: 53px;
    }
    .content1 {
      font-size: 12px;
      h4 {
        text-align: left;
        padding-left: 14px;
        height: 50px;
        line-height: 10px;
        font-weight: 500;
        border-bottom: 1px solid #ccc;
        p {
          margin-top: 10px;
          &:first-of-type {
            color: red;
          }
        }
      }
      .size {
        height: 100px;
        border-bottom: 1px solid #ccc;
        padding: 0 14px;
        p {
          height: 35px;
          &:first-of-type {
            height: 30px;
            line-height: 30px;
            color: #ccc;
            font-size: 14px;
          }
          span {
            display: inline-flex;
            justify-content: center;
            align-items: center;
            padding: 5px 10px 5px 5px;
            background: #eee;
            border-radius: 10px;
            margin-right: 15px;
          }
        }
      }
      .num {
        height: 200px;
        padding: 10px 14px;
        .jiajia {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 20px;
          // border: 1px solid #ddd;
          li {
            border: 1px solid #ddd;
            span {
              display: inline-flex;
              justify-content: center;
              align-items: center;
              width: 25px;
              background: #ddd;
              height: 20px;
            }
            input {
              width: 30px;
              height: 20px;
              border: none;
            }
          }
        }
      }
    }
    .content {
      padding: 0 15px;
      font-size: 14px;
      h4 {
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 16px;
        font-weight: 500;
      }
      p {
        &:first-of-type {
          height: 30px;
          line-height: 30px;
        }
        &:last-of-type {
          height: 250px;
          font-size: 12px;
          color: #ccc;
          line-height: 15px;
        }
      }
    }
    .content2 {
      p {
        width: 100%;
        height: 40px;
        padding: 0 10px;
        font-size: 14px;
        line-height: 40px;
        font-size: 12px;
        border-bottom: 1px solid #eee;
        span {
          display: inline-block;
          width: 100px;
          color: #ccc;
        }
      }
    }
  }
  .active {
    border: 1px solid red;
    background: rgba(255, 0, 0, 0.2) !important;
  }
  .quren {
    border-radius: 20px;
  }
  .store {
    border-bottom: 10px solid #f6f7fb;
    display: flex;
    font-size: 14px;
    height: 90px;
    align-items: center;
    padding: 0 12.5px;
    .left {
      width: 64px;
      height: 64px;
      margin-right: 15px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .center {
      flex: 1;
      font-family: PingFang SC;
      h4 {
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #1c1c1c;
        line-height: 18px;
      }
    }
    .right {
      width: 48px;
      height: 25px;
      border: 1px solid #ccc;
      border-radius: 12.5px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 12px;
      font-family: PingFang SC;
      color: #999999;
      line-height: 18px;
    }
  }
  .van-action-bar {
    z-index: 9;
  }
  .innerdetail {
    margin-bottom: 50px;
    h4 {
      height: 41px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #252525;
      line-height: 18px;
      border-bottom: 1px solid #f6f7fb;
    }
    img {
      width: 100%;
    }
  }
}
</style>
